using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyObj : TankBaseObj
{
    //1. 让坦克在两个点之间 来回移动
    //当前的目标点
    private Transform targetPos;
    //随机用的点 外部去关联
    public Transform[] randomPos;
    //2. 坦克一直盯着自己的目标
    public Transform lookAtTarget;
    //3. 当目标到达一定范围后 间隔一段时间 攻击一下目标
    //开火触发距离
    public float fireDis = 5;
    public float fireOffsetTime = 1;
    private float nowTime = 0;


    //发射位置
    public Transform[] shootPos;
    //子弹预设体
    public GameObject bulletObj;


    // 血条绘制
    public Texture maxHpBk;
    public Texture hpBk;
    private Rect maxHpRect;
    private Rect hpRect;

    private float showHpBarTime;
    private void Start()
    {
        RandomPos();
    }
    private void Update()
    {
        #region 多个点之间的随机逻辑
        //看向自己的目标点
        transform.LookAt(targetPos);
        //不停的向自己的面朝向位移
        transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
        //当距离过小时 认为到达了目的地 从新随机一个点
        if(Vector3.Distance(transform.position, targetPos.position) < 0.05f)
        {
            RandomPos();
        }
        #endregion

        #region 看向自己的目标
        if (lookAtTarget != null)
        {
            tankHead.LookAt(lookAtTarget);
            //当自己和目标的距离小于等于配置距离, 开火
            if (Vector3.Distance(transform.position, lookAtTarget.position) <= fireDis)
            {
                nowTime += Time.deltaTime;
                if (nowTime >= fireOffsetTime)
                {
                    Fire();
                    nowTime = 0;
                }
            }
        }
        #endregion
    }
    private void RandomPos()
    {
        if (randomPos.Length == 0)
            return;
        targetPos = randomPos[Random.Range(0, randomPos.Length)];
    }
    public override void Fire()
    {
        foreach (var item in shootPos)
        {
            GameObject obj = Instantiate(bulletObj, item.position, item.rotation);
            //设置子弹拥有者, 方便进行属性计算
            obj.GetComponent<BulletObj>().setFather(this);
        }
    }
    public override void Dead()
    {
        //加分
        GamePanel.Instance.AddScore(10);
        base.Dead();
    }

    //血条绘制
    private void OnGUI()
    {
        if (showHpBarTime > 0)
        {
            showHpBarTime -= Time.deltaTime;
            //1. 把敌人当前位置转换成屏幕位置
            Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position);
            //2. 屏幕位置转换成 GUI位置
            screenPos.y = Screen.height - screenPos.y;
            //3. 绘制
            maxHpRect.x = screenPos.x - 50;
            maxHpRect.y = screenPos.y - 100;
            maxHpRect.width = 100;
            maxHpRect.height = 15;
            GUI.DrawTexture(maxHpRect, maxHpBk);

            hpRect.x = screenPos.x - 50;
            hpRect.y = screenPos.y - 100;
            hpRect.width = (float)hp / maxHp * 100f;
            hpRect.height = 15;
            GUI.DrawTexture(hpRect, hpBk);
        }

    }

    public override void Wound(TankBaseObj other)
    {
        base.Wound(other);
        //设置显示血条的时间
        showHpBarTime = 3;
    }
}
